package com.lanlian.cayman.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lanlian.commonlib.base.BaseActivity;
import com.lanlian.commonlib.bean.BaseResponse;
import com.lanlian.commonlib.bean.MessageInfo;
import com.lanlian.commonlib.bean.ReqMessage;
import com.lanlian.commonlib.bean.RespMessage;
import com.lanlian.commonlib.bean.db.DBMessage;
import com.lanlian.commonlib.event.EventCode;
import com.lanlian.commonlib.event.EventMessage;
import com.lanlian.commonlib.network.INetWorkCallback;
import com.lanlian.cayman.R;
import com.lanlian.cayman.adapter.MessageAdapter;
import com.lanlian.cayman.utils.VolleyRequestUtils;
import com.lanlian.commonlib.util.EventBusUtils;
import com.lanlian.utillib.CommonUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/8/14 14:26
 *     desc   : 消息中心
 *     version: 1.0
 * </pre>
 */
public class MessageActivity extends BaseActivity {

    private SmartRefreshLayout mLayRefresh;
    private RecyclerView mRecycleView;
    private MessageAdapter mAdapter;
    private List<DBMessage> mList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message, true);
        initView();
        initAdapter();
        setListeners();
        mLayRefresh.autoRefresh();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("消息中心");
        mLayRefresh = findViewById(R.id.lay_refresh);
        mRecycleView = findViewById(R.id.recycle_view);
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mLayRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                getMessageList();
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                mList.get(position).setIsRead(1);
                mDBManager.setMessageList(mList);
                mAdapter.notifyDataSetChanged();
                EventBusUtils.post(new EventMessage(EventCode.EVENT_MESSAGE_POINT));
                startActivity(new Intent(MessageActivity.this, MessageDetailActivity.class)
                        .putExtra("messageInfo", mList.get(position)));
            }
        });
    }

    private void initAdapter() {
        mAdapter = new MessageAdapter(R.layout.adapter_message, mList);
        mRecycleView.setAdapter(mAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecycleView.setLayoutManager(linearLayoutManager);
        mAdapter.bindToRecyclerView(mRecycleView);
        mAdapter.setEmptyView(R.layout.lay_empty_message);
    }

    private void getMessageList() {
        VolleyRequestUtils.getInstance().getMessageList(this.toString(), new ReqMessage(), new INetWorkCallback() {
            @Override
            public void onSuccess(Object response) {
                mLayRefresh.finishRefresh();
                RespMessage respMessage = (RespMessage) response;
                if (!CommonUtils.isListEmpty(respMessage.getInform())) {
                    List<DBMessage> messageList = new ArrayList<>();
                    for (MessageInfo messageInfo : respMessage.getInform()) {
                        messageList.add(new DBMessage(messageInfo));
                    }
                    mDBManager.setMessageList(messageList);
                    mList = mDBManager.getMessageList();
                    mAdapter.setNewData(mList);
                } else {
                    mList = mDBManager.getMessageList();
                    mAdapter.setNewData(mList);
                }
            }

            @Override
            public void onFail(BaseResponse baseResponse) {
                mLayRefresh.finishRefresh();
                mList = mDBManager.getMessageList();
                mAdapter.setNewData(mList);
            }
        });
    }

}
